﻿Imports System.Globalization
Imports System.Reflection
Imports System.Resources

Imports BUS
Imports DTO

Public Class frmThemSachCu

#Region "Đa ngôn ngữ"
    ReadOnly culInfoMgr As CommonLibrary

    Public Sub New(ByVal culInfoMgr As CommonLibrary)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        Me.culInfoMgr = culInfoMgr
        AddHandler culInfoMgr.OnCultureInfoChanges, AddressOf CultureInfiHasChange
    End Sub

    Private Sub CultureInfiHasChange(ByVal sender As Object, ByVal e As EventArgs)
        Try
            SetUI()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub SetUI()
        Dim culInfo As CultureInfo = culInfoMgr.getCultureinfo
        Dim rm As New ResourceManager("Gui.frmThemSachCu", GetType(frmThemSachCu).Assembly)
        Me.Text = rm.GetString("frmThemSachCuText", culInfo)
        lblTenSach.Text = rm.GetString("lblTenSachText", culInfo)
        lblTheLoai.Text = rm.GetString("lblTheLoaiText", culInfo)
        lblTacGia.Text = rm.GetString("lblTacGiaText", culInfo)
        lblSoLuong.Text = rm.GetString("lblSoLuongText", culInfo)
        lblTieuDeThemSach.Text = rm.GetString("lblTieuDeThemSachText", culInfo)
        lblNgayNhap.Text = rm.GetString("lblNgayNhapText", culInfo)
        lblDonGia.Text = rm.GetString("lblDonGiaText", culInfo)

        btnOK.Text = rm.GetString("btnOKText", culInfo)
        btnCancel.Text = rm.GetString("btnCancelText", culInfo)
    End Sub

#End Region

    Dim dt As New DataTable

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub txtSoLuong_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSoLuong.KeyPress
        XuLiHam.ChiNhapSo(sender, e)
    End Sub
    Dim suasach As SachDTO
    Dim loaisua As Integer

    Private Sub frmThemSachCu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            SetUI()
            loaisua = frmMain.loaisua
            suasach = frmMain.suasach
            Dim tentacgia As String = frmMain.tentacgia
            Dim tentheloai As String = frmMain.tentheloai
            If loaisua = 1 Then
                cmbTenSach.Text = suasach.LayTenSach
                cmbTheloai.Text = tentheloai
                cmbTacGia.Text = tentacgia
                txtGiaSach.Text = suasach.LayGiaSach.ToString()

            Else
                LoadTenSach()
                LoadTacGia()
                LoadTheLoai()
                cmbTacGia.DropDownStyle = ComboBoxStyle.DropDownList

                cmbTenSach.DropDownStyle = ComboBoxStyle.DropDownList
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try



    End Sub

    Public Sub LoadTacGia()
        Dim tg As New TacGiaBUS
        dt = tg.LayTacGia()
        cmbTacGia.DataSource = dt
        cmbTacGia.DisplayMember = "TenTacGia"
        cmbTacGia.ValueMember = "idTacGia"
    End Sub

    Public Sub LoadTheLoai()
        Dim theloai As New TheLoaiBUS
        dt = theloai.LayTheLoai()
        cmbTheloai.DataSource = dt
        cmbTheloai.DisplayMember = "TenTheLoai"
        cmbTheloai.ValueMember = "idTheLoai"
    End Sub

    Public Sub LoadTenSach()
        Dim sach As New SachBUS
        dt = sach.LaySach()
        cmbTenSach.DataSource = dt
        cmbTenSach.DisplayMember = "TenSach"
        cmbTenSach.ValueMember = "idSach"


        cmbLayGia_SoLuong.DataSource = dt
        cmbLayGia_SoLuong.DisplayMember = "idTheLoai"
        cmbLayGia_SoLuong.ValueMember = "idSach"
        '  txtGiaSach.Text = 
    End Sub

    Private Sub cmbTenSach_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbTenSach.TextChanged
      

    End Sub

    Private Sub cmbLayGia_SoLuong_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbLayGia_SoLuong.TextChanged
        Dim theloai As New TheLoaiDTO
        Dim tg As New TacGiaDTO
        Dim sach As New SachDTO
        Dim s As New SachBUS
        'sach.LayIdSach = cbTenSach.SelectedValue
        sach.LayIdSach = cmbTenSach.SelectedValue

        Dim k As New DataTable
        k = s.LayTenSach(sach)  'SachDAO.LayTenSach(sach)
        theloai.LayIdTheLoai = k(0)("idTheLoai")

        Dim theloai1 As New TheLoaiBUS

        Dim r As DataTable = theloai1.LayTheLoaiTheoMa(theloai)
        cmbTheloai.Text = r(0)("TenTheLoai")
        'tg.LayIdTacGia = k(0)("idTacGia")

        'Dim tacgia As New TacGiaBUS
        'Dim t As DataTable = tacgia.LayTenTacGia(tg)
        'cmbTacGia.Text = t(0)("TenTacGia")


        sach.LayIdSach = Integer.Parse(cmbTenSach.SelectedValue)

        txtGiaSach.Text = s.LayGia(sach).ToString()
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Try

            'Dim sachDTO As New SachDTO
            'Dim sachBUS As New SachBUS

            'Dim cccDTO As New ChiTietHoaDonDTO
            'Dim cccDAO As New ChiTietSachBUS

            'cccDTO.LayIDSach = cmbTenSach.SelectedValue
            'sachDTO.LayIdSach = cmbTenSach.SelectedValue
            'sachDTO.LayNgayNhap = DateTimeInput1.Value


            'Dim dt As New DataTable
            'dt = sachBUS.LayTenSach(sachDTO)

            'DataGridViewX1.DataSource = dt

            'Dim soluongsach As Integer
            'soluongsach = Integer.Parse(dt.Rows(0).ItemArray(7).ToString)


            Dim sach As New SachDTO
            If loaisua = 1 Then
                sach.LayTenSach = cmbTenSach.Text
                sach.LayGiaSach = txtGiaSach.Text
                sach.LayNgayNhap = DateTimeInput1.Value.Date
                sach.LaySoLuong = txtSoLuong.Text
                sach.LayIdSach = suasach.LayIdSach
                sach.layTacGia = suasach.layTacGia
                sach.LayIDTheLoai = suasach.LayIDTheLoai
            Else

                'Dim cts As New ChiTietSachDTO
                sach.LayTenSach = cmbTenSach.Text '
                sach.LayIDTheLoai = Integer.Parse(cmbTheloai.SelectedValue)
                sach.LayGiaSach = txtGiaSach.Text '
                sach.LayNgayNhap = DateTimeInput1.Value.Date '
                sach.LaySoLuong = txtSoLuong.Text '
                sach.layTacGia = cmbTacGia.SelectedValue
                sach.LayIdSach = cmbTenSach.SelectedValue

            End If

            Dim ns As New SachBUS
            Dim ct As New ChiTietSachDTO
            ct.laytonDau = txtSoLuong.Text
            ct.LayToncuoi = txtSoLuong.Text
            ct.LayIDSach = sach.LayIdSach
            ns.NhapSachTonTai(sach, ct)

            Me.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

      
    End Sub

    Private Sub cmbTenSach_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbTenSach.SelectedIndexChanged

    End Sub

    Private Sub cmbTheloai_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbTheloai.SelectedIndexChanged

    End Sub
End Class